import { bench, describe } from 'vitest';
import { batchMeasureStrings, clearStringMeasurementCache, getStringSize } from './domUtils';

export const strings = [
  'Aruba',
  'Afghanistan',
  'Angola',
  'Albania',
  'Andorra',
  'United Arab Emirates',
  'Argentina',
  'Armenia',
  'American Samoa',
  'Antigua and Barbuda',
  'Australia',
  'Austria',
  'Azerbaijan',
  'Burundi',
  'Belgium',
  'Benin',
  'Burkina Faso',
  'Bangladesh',
  'Bulgaria',
  'Bahrain',
  'Bahamas, The',
  'Bosnia and Herzegovina',
  'Belarus',
  'Belize',
  'Bermuda',
  'Bolivia',
  'Brazil',
  'Barbados',
  'Brunei Darussalam',
  'Bhutan',
  'Botswana',
  'Central African Republic',
  'Canada',
  'Switzerland',
  'Channel Islands',
  'Chile',
  'China',
  "Cote d'Ivoire",
  'Cameroon',
  'Congo, Dem. Rep.',
  'Congo, Rep.',
  'Cook Islands',
  'Colombia',
  'Comoros',
  'Cabo Verde',
  'Costa Rica',
  'Cuba',
  'Curacao',
  'Cayman Islands',
  'Cyprus',
  'Czechia',
  'Germany',
  'Djibouti',
  'Dominica',
  'Denmark',
  'Dominican Republic',
  'Algeria',
  'Ecuador',
  'Egypt',
  'Eritrea',
  'Spain',
  'Estonia',
  'Ethiopia',
  'Finland',
  'Fiji',
  'France',
  'Faroe Islands',
  'Micronesia',
  'Gabon',
  'United Kingdom',
  'Georgia',
  'Ghana',
  'Guinea',
  'Gambia, The',
  'Guinea-Bissau',
  'Equatorial Guinea',
  'Greece',
  'Grenada',
  'Greenland',
  'Guatemala',
  'Guam',
  'Guyana',
  'Hong Kong',
  'Honduras',
  'Croatia',
  'Haiti',
  'Hungary',
  'Indonesia',
  'Isle of Man',
  'India',
  'Ireland',
  'Iran',
  'Iraq',
  'Iceland',
  'Israel',
  'Italy',
  'Jamaica',
  'Jordan',
  'Japan',
  'Kazakhstan',
  'Kenya',
  'Kyrgyzstan',
  'Cambodia',
  'Kiribati',
  'St. Kitts and Nevis',
  'South Korea',
  'Kuwait',
  'Laos',
  'Lebanon',
  'Liberia',
  'Libya',
  'St. Lucia',
  'Liechtenstein',
  'Sri Lanka',
  'Lesotho',
  'Lithuania',
  'Luxembourg',
  'Latvia',
  'Macao SAR, China',
  'St. Martin (French part)',
  'Morocco',
  'Monaco',
  'Moldova',
  'Madagascar',
  'Maldives',
  'Mexico',
  'Marshall Islands',
  'North Macedonia',
  'Mali',
  'Malta',
  'Myanmar',
  'Montenegro',
  'Mongolia',
  'Northern Mariana Islands',
  'Mozambique',
  'Mauritania',
  'Mauritius',
  'Malawi',
  'Malaysia',
  'Namibia',
  'New Caledonia',
  'Niger',
  'Nigeria',
  'Nicaragua',
  'Niue',
  'Netherlands',
  'Norway',
  'Nepal',
  'Nauru',
  'New Zealand',
  'Oman',
  'Pakistan',
  'Panama',
  'Peru',
  'Philippines',
  'Palau',
  'Papua New Guinea',
  'Poland',
  'Puerto Rico',
  'North Korea',
  'Portugal',
  'Paraguay',
  'West Bank and Gaza',
  'French Polynesia',
  'Qatar',
  'Romania',
  'Russia',
  'Rwanda',
  'Saudi Arabia',
  'Sudan',
  'Senegal',
  'Singapore',
  'Solomon Islands',
  'Sierra Leone',
  'El Salvador',
  'San Marino',
  'Somalia',
  'Serbia',
  'Sao Tome and Principe',
  'Suriname',
  'Slovakia',
  'Slovenia',
  'Sweden',
  'Eswatini',
  'Sint Maarten (Dutch part)',
  'Seychelles',
  'Syria',
  'Turks and Caicos Islands',
  'Chad',
  'Togo',
  'Thailand',
  'Tajikistan',
  'Tokelau',
  'Turkmenistan',
  'Timor-Leste',
  'Tonga',
  'Trinidad and Tobago',
  'Tunisia',
  'Turkiye',
  'Tuvalu',
  'Taiwan',
  'Tanzania',
  'Uganda',
  'Ukraine',
  'Uruguay',
  'United States',
  'Uzbekistan',
  'St. Vincent and the Grenadines',
  'Venezuela',
  'Virgin Islands (U.S.)',
  'Viet Nam',
  'Vanuatu',
  'Samoa',
  'Kosovo',
  'Yemen, Rep.',
  'South Africa',
  'Zambia',
  'Zimbabwe',
];

const baseDate = new Date(1760670813770);
for (let i = 0; i < 10_000; i += 1) {
  const date = new Date(baseDate);
  date.setDate(i + 1);
  strings.push(date.toISOString());
}

const setup = () => {
  // Call once to create the measuring dom element
  getStringSize('Warmup');
  clearStringMeasurementCache();
};

describe('getStringSize', () => {
  bench(
    'without styles',
    () => {
      strings.forEach((countryName) => getStringSize(countryName));
    },
    { setup },
  );

  bench(
    'with alternating styles',
    () => {
      const style1 = { fontSize: 12, fontFamily: 'Arial' };
      const style2 = { fontSize: 16, fontFamily: 'Times New Roman', fontWeight: 'bold' };
      const firstHalf = strings.filter((_, i) => i % 2 === 0);
      const secondHalf = strings.filter((_, i) => i % 2 !== 0);

      firstHalf.forEach((countryName) => getStringSize(countryName, style1));
      secondHalf.forEach((countryName) => getStringSize(countryName, style2));
    },
    { setup },
  );
});

describe('batchMeasureStrings', () => {
  bench(
    'without styles',
    () => {
      batchMeasureStrings(strings);
    },
    { setup },
  );

  bench(
    'with alternating styles',
    () => {
      const style1 = { fontSize: 12, fontFamily: 'Arial' };
      const style2 = { fontSize: 16, fontFamily: 'Times New Roman', fontWeight: 'bold' };
      const firstHalf = strings.filter((_, i) => i % 2 === 0);
      const secondHalf = strings.filter((_, i) => i % 2 !== 0);

      batchMeasureStrings(firstHalf, style1);
      batchMeasureStrings(secondHalf, style2);
    },
    { setup },
  );
});
